<?php

namespace App\Models\Banners;

use App\Services\Utils\ImageHelper;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;

/**
 * @method static self filterBannerStatus($banner_id)
 *
 * Class BannerImage
 * @package App\Models\Banners
 * @mixin Builder
 */

class BannerImage extends Model
{
    protected $table = 'banner_image';
    protected $guarded = [];

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function banner()
    {
        return $this->belongsTo(Banner::class, 'banner_id', 'banner_id');
    }

    /**
     * @used-by filterBannerStatus()
     *
     * @param Builder $query
     * @param $banner_id
     * @return Builder
     */
    public function scopeFilterBannerStatus(Builder $query, $banner_id)
    {
        return $query->whereHas('banner', function(Builder $query) use($banner_id){
            return $query->where('status', Banner::BANNER_STATUS_ON)
                ->where('banner_id', $banner_id);
        });
    }

    /**
     * @return string
     */
    public function getImageAttribute()
    {
        $route = $this->attributes['image'];

        if ($route) {
            return ImageHelper::generateImagePath($route);
        } else {
            return null;
        }
    }

    /**
     * @return null|string
     */
    public function getTitleAttribute()
    {
        $title = $this->attributes['title'];

        if ($title) {
            return html_entity_decode($title, ENT_QUOTES, 'UTF-8');
        } else {
            return null;
        }
    }

    /**
     * @return null|string
     */
    public function getLinkAttribute()
    {
        $link = $this->attributes['link'];

        if ($link) {
            return html_entity_decode($link, ENT_QUOTES, 'UTF-8');
        } else {
            return null;
        }
    }
}
